package com.sc.nft.controller;

import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.ExchangeRequest;
import com.sc.nft.controller.request.IdRequest;
import com.sc.nft.entity.req.ActivityPropsUpRequest;
import com.sc.nft.entity.vo.ActivityPropsExchangeDetailVO;
import com.sc.nft.entity.vo.ActivityPropsUpVO;
import com.sc.nft.enums.BoxTakePoolConvertScenarioEnum;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.ActivityPropsService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.TimeUnit;


@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/activityProps")
@Api(tags = "活动道具卡兑换")
public class ActivityPropsController {

    private final ActivityPropsService activityPropsService;

    private final Redisson redisson;

    @ApiOperation(value = "获取所有上架道具卡", notes = "获取所有上架道具卡", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getUpActivityPropsList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<List<ActivityPropsUpVO>> getUpActivityPropsList(@RequestBody @Valid ActivityPropsUpRequest request) {
        if (Objects.equals(request.getConvertScenario(), BoxTakePoolConvertScenarioEnum.DAO_CONVERT)
                && Objects.isNull(request.getDaoId())) {
            throw new GlobalRunTimeException("品牌不能为空");
        }
        return PageResult.success(activityPropsService.getUpActivityPropsList(request.getConvertScenario(), request.getDaoId(), request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "获取道具卡详情", notes = "获取道具卡详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getActivityPropsDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<ActivityPropsExchangeDetailVO> getActivityPropsDetail(@RequestBody IdRequest request) {
        return Result.success(activityPropsService.getActivityPropsDetail(request.getId()));
    }

    @ApiOperation(value = "兑换", notes = "兑换", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/exchange", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result exchange(@Valid @RequestBody ExchangeRequest request) {
        RLock lock = redisson.getLock(RedisKeyEnum.EXCHANGE_ACTIVITY_PROPS.getKey() + request.getId());
        try {
            boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
            if (!request.getPaymentPassword().equals(UserPool.getUser().getTransactionPassword())) {
                throw new GlobalRunTimeException("支付密码错误");
            }
            if (res) {
                activityPropsService.exchange(request.getId(), request.getNum());
            } else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            return Result.fail("当前人数过多，请稍后再试");
        } finally {
            lock.unlock();
        }
        return Result.success();
    }

}
